package tsb.dominio.interfaces;

import tsb.dominio.excepciones.MACException;
import java.util.Random;

/**
 * @author Federico Gomez
 * @version 1.0
 * @created 13-Mar-2011 07:22:33 p.m.
 */
public class MAC {

    /**
     * mac. guarda el string con la MAC address
     */
    private String mac;
    
    public MAC(String mac) throws MACException{
            try{
                if(verificarMAC(mac))
                {
                    this.mac=mac;
                }
            }
            catch(MACException m)
            {
                throw m;
            }

        }
    
    public static boolean verificarMAC(String mac) throws MACException // AA-AA-AA-AA-AA-AA
        {
            String correct="0123456789ABCDEF-";
            
            String [] a= mac.split("");

            if(!(a.length==18))
                 throw new MACException();

            for(int i=0;i<a.length;i++)
            {
                 if(!correct.contains(a[i]))
                    throw new MACException();
                 
            }
             return true;
        }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }
    
    public static String generarMAC()
    {
        Random r = new Random();
        String mac="";

        for(int i=0; i<12;i++)
        {
            if(i==0)//0 1 8 A
            {
                while(true)
                {
                    int v= r.nextInt(11);
                    String a = getValor(v);
                    if(a.compareTo("0")==0 ||a.compareTo("1")==0 || a.compareTo("8")==0 || a.compareTo("A")==0)
                    {
                        mac+=a;
                        break;
                    }
                }
            }
            else
            {
                if(i==1)//0 2 4 8 A
                {
                    while(true)
                    {
                        int v= r.nextInt(11);
                        String a = getValor(v);
                        if(a.compareTo("0")==0 ||a.compareTo("2")==0 ||a.compareTo("4")==0 || a.compareTo("8")==0 || a.compareTo("A")==0)
                        {
                            mac+=a;
                            break;
                        }
                    }
                }
                else
                {
                    int v= r.nextInt(16);
                    String a = getValor(v);
                    mac+=a;
                }
            }

            if(i%2!=0 && i!=11)
                mac+="-";
        }

        return mac;
    }
    
    private static String getValor(int pos)
    {
        switch(pos)
        {
                case 0: return "0";
                case 1: return "1";
                case 2: return "2";
                case 3: return "3";
                case 4: return "4";
                case 5: return "5";
                case 6: return "6";
                case 7: return "7";
                case 8: return "8";
                case 9: return "9";
                case 10: return "A";
                case 11: return "B";
                case 12: return "C";
                case 13: return "D";
                case 14: return "E";
                case 15: return "F";
        }
         return "";
    }
    
    public String toString()
    {
        return this.mac;
    }

	

}